package modes

import (
	"fmt"
	"time"
)

/*
 * 描述：拨打记录
 *
 ********************************************************************************************/
type CallPhone struct {
	Id      int64  `json:"call_id" xorm:"id"`                 // 拨打记录id
	UId     int64  `json:"call_uid" xorm:"uid"`               // 拨打老师id
	TQBill  string `json:"call_tq" xorm:"tq_id"`              // TQMessageId
	Weight  int64  `json:"call_weight" xorm:"weight"`         // 权重
	Phone   int64  `json:"call_phone" xorm:"phone"`           // 手机号
	At      int64  `json:"call_at" xorm:"at"`                 // 拨打时间
	SouId   int64  `json:"call_source_id" xorm:"source_id"`   // 来源 id
	PId     int64  `json:"call_pid" xorm:"pid"`               // 项   目   ID
	UNote   string `json:"call_u_note" xorm:"u_note"`         // 备注/评价
	Duratio int64  `json:"call_duration" xorm:"duration"`     // 通 话  时 长
	Ring    int64  `json:"call_ring" xorm:"ring"`             // 响 铃  时 长
	Area    string `json:"call_area" xorm:"area"`             // 归   属   地
	FRecord string `json:"call_f_record" xorm:"f_record"`     // 录 音  地 址
	CallSty int64  `json:"call_call_style" xorm:"call_style"` // 呼 叫 类  型
	Fage    int64  `json:"call_fage" xorm:"fage"`             // 标        签
	State   int64  `json:"call_state" xorm:"state"`           // 状        态
}
type PhoneTime struct {
	Total string `json:"total"`
	UId   string `json:"uid"`
}

func (this *CallPhone) TableName() string {
	return "call_record"
}

func (this *CallPhone) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(2).Insert(this)
}

func (this *CallPhone) Get() (bool, error) {
	return Db(2).Get(this)
}

func (this *CallPhone) update(where string, field string) (int64, error) {
	return Db(2).Where(where).Cols(field).Update(this)
}

func (this *CallPhone) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *CallPhone) count(where string) (int64, error) {
	return Db(2).Where(where).Count(this)
}

func (this *CallPhone) Count(where string) (int64, error) {
	return Db(2).Where(where).Count(this)
}

func (this *PhoneTime) TimeTj(uid, s_at, e_at int64) ([]PhoneTime, error) {
	list := make([]PhoneTime, 0)
	TName := "call_record"
	selec := fmt.Sprintf("SELECT uid,SUM(duration) AS c_time from %s", TName)
	where := fmt.Sprintf("WHERE uid = %d and at >= %d and at <= %d ", uid, s_at, e_at)
	group := fmt.Sprintf("GROUP BY uid")
	sil, err := Db(2).Query(fmt.Sprintf("%s %s %s", selec, where, group))
	if nil == err {
		for i, _ := range sil {
			this.UId = string(sil[i]["uid"])
			this.Total = string(sil[i]["c_time"])
			list = append(list, *this)
		}
	}
	return list, err
}

func (this *CallPhone) Where(fage, count, page int, where, field string) ([]CallPhone, error) {
	list := make([]CallPhone, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	fmt.Println("---------------------------------------------------", this.UId)
	if 0 == fage { // 从小到大排序
		err = Db(2).Table(this.TableName()).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(2).Table(this.TableName()).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

func (this *CallPhone) EList(val *Pager) error {
	var field string
	switch val.Coll {
	case 0:
		field = "at" // 拨打时间
	case 1:
		field = "weight" // 权重
	}
	list, err := this.Where(val.Rules, val.Count, val.Page, val.ToWhere(), field)
	val.Total, _ = this.count(val.ToWhere())
	if err == nil {
		val.List = list
	}
	return err
}

/*
 * 描述: 电话记录列表
 *
 ***************************************************************************************/
func (this *CallPhone) PList(uid int64, val *Pager) error {
	list := make([]CallPhone, 0)
	val.TName = "call_record"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 拨打时间
	case 1:
		field = fmt.Sprintf("%s.duration", val.TName) // 通话时长
	case 2:
		field = fmt.Sprintf("%s.pid", val.TName) // 项目
	case 3:
		field = fmt.Sprintf("%s.fage", val.TName) // 标签
	case 4:
		field = fmt.Sprintf("%s.state", val.TName) // 状态
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 拨打时间
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(2).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(2).Table(val.TName).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}
